"use strict";
module.exports = (sequelize, DataTypes) => {
  const GoodsSku = sequelize.define(
    "GoodsSku",
    {
      id: {
        type: DataTypes.INTEGER, // 主键声明
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
      },
      goods_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
      },
      sku_name: DataTypes.STRING,
      price: DataTypes.DECIMAL(10, 2),
      oprice: DataTypes.DECIMAL(10, 2),
      stock: DataTypes.INTEGER,
      image: DataTypes.STRING,
    },
    {
      tableName: "goods_skus",
      underscored: true,
      timestamps: true,
    }
  );

  GoodsSku.associate = function (models) {
    GoodsSku.belongsTo(models.Goods, {
      foreignKey: "goods_id",
      as: "goods",
    });
  };
  if (process.env.SYNC_MODE === "true") {
    GoodsSku.sync({ alter: true })
      .then(() => console.log("✅ GoodsSku表同步成功"))
      .catch((err) => console.error("❌ GoodsSku表同步失败:", err));
  }
  return GoodsSku;
};
